package com.zone.gateway.core.bind;

import com.zone.gateway.core.session.GatewaySession;
import com.zone.gateway.core.mapping.HttpStatement;
import com.zone.gateway.core.session.Configuration;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: zongzi
 * @description: 泛化服务注册器
 * @date: 2025/4/17
 */
public class MappedRegister {
    // session 上下文
    private final Configuration configuration;

    private final Map<String, MappedProxyFactory> knowledge = new ConcurrentHashMap<>();

    public MappedRegister(Configuration configuration) {
        this.configuration = configuration;
    }

    public IGenericReference getMapped(String uri, GatewaySession gatewaySession) {
        MappedProxyFactory cacheFactory = knowledge.get(uri);
        if (null == cacheFactory) {
            throw new RuntimeException("uri not registered yet = " + uri);
        }
        return cacheFactory.newInstant(gatewaySession);
    }

    public void regis(HttpStatement statement) {
        MappedProxyFactory genericReferenceProxyFactory = new MappedProxyFactory(statement.getUri());
        knowledge.put(statement.getUri(), genericReferenceProxyFactory);
    }
}
